package com.example.wisdomclassroomapi.application.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.wisdomclassroomapi.common.util.IdentificationUtil;
import com.example.wisdomclassroomapi.domain.entity.CourseLessonPeriodEntity;
import com.example.wisdomclassroomapi.domain.service.CourseLessonPeriodDomainService;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Package: com.example.wisdomclassroomapi.application.service
 * @Description: <>
 * @Author: scholes
 * @CreateDate: 2022-04-18
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-18
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Service
public class CourseLessonPeriodApplicationService {
    @Resource
    private CourseLessonPeriodDomainService courseLessonPeriodDomainService;

    public void add(CourseLessonPeriodEntity entity) {
        courseLessonPeriodDomainService.add(entity);
    }

    public CourseLessonPeriodEntity findById(String id) {
        return courseLessonPeriodDomainService.findById(id);
    }

    public int updateById(CourseLessonPeriodEntity entity) {
        return courseLessonPeriodDomainService.updateById(entity);
    }

    public int deleteById(String id) {
        return courseLessonPeriodDomainService.deleteById(id);
    }

    public IPage<CourseLessonPeriodEntity> page(CourseLessonPeriodEntity entity, IPage page) {
        return courseLessonPeriodDomainService.page(entity, page);
    }

    public List<CourseLessonPeriodEntity> list(CourseLessonPeriodEntity entity) {
        return courseLessonPeriodDomainService.list(entity);
    }

    public Long getVideoTime(MultipartFile multipartFile) throws IOException, EncoderException {
        String name = IdentificationUtil.shortUuid();
        File file = new File("D:\\Develop\\docker\\TMP\\"+name);
        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
        MultimediaObject instance = new MultimediaObject(file);
        MultimediaInfo result = instance.getInfo();
        long videoTime = result.getDuration() / 1000;
        file.delete();
        return videoTime;
    }

}